package javaer.starter.core;

import javaer.starter.base.domain.dto.NewProjectDefinition;
import javaer.starter.constant.StorePathEnum;
import javaer.starter.core.generator.IGenerator;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yangzhen
 * @date 2023/6/7 17:41
 * @since 1.0.0
 */
@Component
public class Bootstrap {


    @Resource
    private ProjectDefinitionParser projectDefinitionParser;

    @Resource
    private List<IGenerator> generators;

    /**
     * 生成项目并返回下载地址
     *
     * @param projectDefinition
     * @return
     * @throws Exception
     */
    public BuildContext generate(NewProjectDefinition projectDefinition) throws Exception {
        BuildContext buildContext = projectDefinitionParser.parse(projectDefinition);
        buildContext.setProjectPath(StorePathEnum.PROJECT_PATH.getPath().resolve(buildContext.getProjectName()));
        buildContext.setTemplatePath(StorePathEnum.TEMPLATE_PATH.getPath().resolve(buildContext.getTypeName()));
        for (IGenerator generator : generators) {
            generator.generate(buildContext);
        }
        return buildContext;
    }

}
